<?php

namespace app\admin\controller;

use think\Db;
use think\Validate;
class Customer extends Base{
	public function index(){
		return $this->fetch();
	}
	public function index_data(){
		$input = input();
		$where=[];
		if (isset($input['keywords']) and !empty($input['keywords'])) {
			$where[] = ['u.username','like', '%' . $input['keywords'] . '%'];
		}
		
		if (isset($input['seach_time']) and !empty($input['seach_time'])) {
			$str = explode('~',$input['seach_time']);
		    $star_time=$str[0]? $str[0] : '';
		    $end_time=$str[1] ? $str[1] : '';
			$where[]=['u.add_time','between',[strtotime($star_time),strtotime($end_time)+86400]];
			
		}
		$page       = ($input['page']-1)*$input['limit'];
		$limit      = $input['limit'];
		$data = Db::name('user')->alias('u')
        ->join('label l','u.label=l.id','LEFT')
		->where('u.identity','=',1)
		->where($where)
		->order('u.add_time desc')
		->field('u.*,l.name type_name')
		->limit($page,$limit)
		->select();
		
		$count = Db::name('user')->alias('u')
		->join('label l','u.label=l.id','LEFT')
		->where($where)
		->where('u.identity','=',1)
		->count();
		
		return hSuccess('OK', $data,$count,ceil($count/$limit));
	}
	
	public function add(){
		if($this->request->isPost()) {
			//是提交操作
			$post = $this->request->post();
			$rule = [
			    'title'   => 'require',
			    's_title' => 'require',
			];
			$msg = [
			    'title.require'  => '大标题不能为空',
			    's_title.require'  	=> '标题不能为空',
			];
			$validate   = Validate::make($rule,$msg);
		    //验证部分数据合法性
		    if (!$validate->check($post)) {
		        $this->error('提交失败：' . $validate->getError());
			}
			$post['add_time'] 	= time();
			$post['type'] 	= 0;
			
			$res = Db::name('user')->insert($post);
		    if($res) {
		    	return hSuccess('操作成功');
		    } else {
				return hError(302,'操作失败');
		    }
		}
		$info = Db::name('label')->where('status',1)->order('sort asc')->select();
		$this->assign('type',$info);
		return $this->fetch();
	}
	public function edit(){
		// 获取参数
		$param = $this->request->param();
		// 提交操作
		if($this->request->isAjax()){
			// var_dump($param);die;
			$data = [
				'label'		=>	$param['staff_type'],
				'username'	=>	$param['username'],
				'address'	=>	$param['address'],
				'budget'	=>	$param['budget'],
				'score'	=>	$param['score'],
				'update_time'	=>	time(),
			];
			
			$res = Db::name('user')->where('id',$param['id'])->update($data);
	        if($res){
	            return hSuccess('操作成功');
	        }else{
	            return hError('-1','操作失败');
	        }
		}
		// 展示界面
		$info = Db::name('user')->find($param['id']);
		$this->assign('info',$info);
		$info = Db::name('label')->where('status',1)->order('sort asc')->select();
		$this->assign('type',$info);
	    return $this->fetch();
	}
	public function reset_pwd(){
		$input = $this->request->param();
		if($this->request->isAjax()) {
			$id = $this->request->has('id') ? $this->request->param('id', 0, 'intval') : 0;
			$mobile = Db::name('user')->where('id',$id)->value('mobile');
			$res = Db::name('user')->where('id',$id)->update(['password'=>md5($mobile),'update_time'=>time()]);
			if($res) {
				return hSuccess('重置成功');
			} else {
				return hError(410,'重置失败');
			}
		}
		
	}
	public function delete(){
		$input = $this->request->param();
		if($this->request->isAjax()) {
			$id = $this->request->has('id') ? $this->request->param('id', 0, 'intval') : 0;
			$res = Db::name('user')->where('id',$id)->delete();
			if($res) {
				return hSuccess('删除成功');
			} else {
				return hError(410,'删除失败');
			}
		}
	}
	
	public function type(){
		return $this->fetch();
	}
	public function type_data(){
		$input = input();
		$where=[];
		if (isset($post['keywords']) and !empty($post['keywords'])) {
			$where['name|clientId'] = ['like', '%' . $post['keywords'] . '%'];
		}
		if (isset($post['seach_time']) and !empty($post['seach_time'])) {
			$str = explode('~',$time);
		    $star_time=$str[0]? $str[0] : '';
		    $end_time=$str[1] ? $str[1] : '';
		    $where['add_time']=['between',[strtotime($star_time),strtotime($end_time)+86400]];
		}
		$page       = ($input['page']-1)*$input['limit'];
		$limit      = $input['limit'];
		
		$data = Db::name('label')
		->where($where)
		->order('sort asc')
		->limit($page,$limit)
		->select();
		// var_dump($where);die;
		$count = Db::name('label')
		->where($where)
		->count();
		
		return hSuccess('OK', $data,$count,ceil($count/$limit));
	}
	public function type_add(){
		if($this->request->isPost()) {
			//是提交操作
			$post = $this->request->post();
			
			$rule = [
			    'name'   => 'require',
			];
			$msg = [
			    'name.require'  => '名称不能为空',
			];
			$validate   = Validate::make($rule,$msg);
		    //验证部分数据合法性
		    if (!$validate->check($post)) {
		        $this->error('提交失败：' . $validate->getError());
		    }
			$post['add_time'] 	= time();
			
			$res = Db::name('label')->insert($post);
		    if($res) {
		    	return hSuccess('操作成功');
		    } else {
				return hError(302,'操作失败');
		    }
		}
		
		return $this->fetch();
	}
	public function type_edit(){
		// 获取参数
		$param = $this->request->param();
		// 提交操作
		if($this->request->isAjax()){
			
			$data = [
				'name'		=>	$param['name'],
				'is_show'	=>	isset($param['is_show']) ?$param['is_show'] :0,
				'status'	=>	isset($param['status']) ?$param['status'] :0,
				'sort'	=>	$param['sort'],
				'update_time'	=>	time(),
			];
			
			$res = Db::name('label')->where('id',$param['id'])->update($data);
	        if($res){
	            return hSuccess('操作成功');
	        }else{
	            return hError('-1','操作失败');
	        }
		}
		// 展示界面
		$info = Db::name('label')->find($param['id']);
		$this->assign('info',$info);
	    return $this->fetch();
	}
	public function type_delete(){
		$input = $this->request->param();
		if($this->request->isAjax()) {
			$id = $this->request->has('id') ? $this->request->param('id', 0, 'intval') : 0;
			$res = Db::name('label')->where('id',$id)->delete();
			if($res) {
				return hSuccess('删除成功');
			} else {
				return hError(410,'删除失败');
			}
		}
	}
}
?>